package cn.myjerry.net.udp;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;

/**
 * 客户端
 * 1、创建客户端
 * 2、准备数据	double --> 字节数组
 * 3、打包
 * 4、发送
 * 5、释放
 * @author Jerry
 *
 */
public class Client {
	public static void main(String[] args) throws IOException {
		DatagramSocket client = new DatagramSocket(6666);
		
		//String msg = "test data";
		//byte[] b = msg.getBytes();
		
		byte[] b = null;
		double num = 12.09;
		b = convert(num);
		
		DatagramPacket packet = new DatagramPacket(b, 0, b.length, new InetSocketAddress("localhost", 8888));
		
		client.send(packet);
		
		client.close();
	}
	
	public static byte[] convert(double num) throws IOException {
		byte[] data = null;
		
		ByteArrayOutputStream  bos = new ByteArrayOutputStream();
		DataOutputStream dos = new DataOutputStream(bos);
		
		dos.writeDouble(num);
		dos.flush();
		
		data = bos.toByteArray();
		
		return data;
	}
}
